package com.logic.landseaserver.ws;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.LandeaConstants.CheckType;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.service.ICheckListService;
import com.logic.landseaserver.ws.dto.CheckListDTO;
import com.logic.landseaserver.ws.dto.CheckListProjectTypeDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Author: jack
 * @Date: 2017/6/16
 * @Description: 交割单
 */
@Path("/checkList")
@Api(value="checkList")
public class CheckListRest {

	@Autowired
	private ICheckListService iCheckListService;
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
    //查询详情
	@GET
    @Path("/{checkListId}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Get checkList detail ", position = 4)
    public Response getCheckListById(
            @ApiParam(value = "param", required = true) @PathParam("checkListId") Integer checkListId) {
        CommonResultMap map = new CommonResultMap();
        try {
        	CheckListDTO  ch = iCheckListService.getCheckListDitail(checkListId, CheckType.IN_COMING);
        	map.setData(ch);
        } catch (Exception e) {
        	String str = e.getClass().getName();
        	//如果查询不到数据返回为空。
        	if("java.lang.NullPointerException".equals(str)){
        		 map.setData(null);
        	}
        	logger.error(" getCheckListById error", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
	

    // 增加
    @POST
    @Path("/createCheckList")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "Create a new checkList", position = 1)
    public Response createCheckList(@ApiParam(value = "param", required = true) CheckListDTO checkListDTO)
    {
        CommonResultMap map = new CommonResultMap();
        Map<String, Object> result = new HashMap<String, Object>();
        try
        {
            // 此处只有入住交割单用
            checkListDTO.setCheckType(CheckType.IN_COMING);
            int id = iCheckListService.insertCheckList(checkListDTO);
            if (id == 7010001)
            {
                result.put("STATUS", 7010001);
                result.put("MSG", "已经存在该记录，无法重复提交。");
                map.setData(result);
            }
            else
            {
                result.put("STATUS", 0);
                result.put("MSG", "Create success");
                map.setData(result);
            }
        }
        catch (LandseaException e)
        {
            LoggerHelper.handleException(logger, "editCheckList", e, map);
        }
        catch (BusinessException e)
        {
            result.put("STATUS", -2);
            result.put("MSG", "UNKNOWN ERROR");
            map.setData(result);
            logger.error(" createCheckList error", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        }
        catch (Exception e)
        {
            result.put("STATUS", -2);
            result.put("MSG", "UNKNOWN ERROR");
            map.setData(result);
            logger.error(" createCheckList error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
	
    //编辑
    @PUT
	@Path("/editCheckList")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Edit CheckList ")
    public Response editCheckList(@ApiParam(value = "param", required = true) CheckListDTO checkListDTO) {
        CommonResultMap map = new CommonResultMap();
        try {
        	iCheckListService.editCheckList(checkListDTO);
            map.setResult(CommonResult.SUCCESS,"edit Deposit");
        } 
        catch (LandseaException e)
        {
            LoggerHelper.handleException(logger, "editCheckList ", e, map);
        }
        catch (Exception e) {
            logger.error("editCheckList error", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
    //根据合同查询项目对应的费用类型
  	@POST
  	@Path("/getProductForType")
  	@Produces(MediaType.APPLICATION_JSON)
  	@Consumes(MediaType.APPLICATION_JSON)
  	@ApiOperation(value = "根据合同查询项目对应的费用类型{contractId:xx}",position = 4)
  	public Response getProductForType(@ApiParam(value = "param", required = true) Map pmap) {
  	     CommonResultMap map = new CommonResultMap();
  	     Map<String, Object> result = new HashMap<String, Object>();
  	     
  	     String con = (String)pmap.get("contractId");
  	     Integer in = Integer.parseInt(con);
  	     try {
  	    	  List<CheckListProjectTypeDTO> projects =  iCheckListService.getProIdByContract(in);
  	    	  result.put("total", projects.size());
          	  result.put("projects", projects);
              map.setData(result);
          } catch (Exception e) {
              logger.error("getProductForType error", e);
          }
          return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();		
  	}

}
